﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Walson.Scientific
{
    /// <summary>
    /// 信号
    /// </summary>
    static public class Signals
    {
        /// <summary>
        /// 正弦信号
        /// </summary>
        /// <param name="sampleRate">采样倍数</param>
        /// <param name="fi">初始相位</param>
        /// <returns></returns>
        static public List<double> Sin(int sampleRate,double fi)
        {
            int frequency = 1;

            List<double> result = new List<double>();

            double delta = 1.0 / frequency / sampleRate;
            double Ts = 1.0 / frequency;
            double c = Math.PI * 2.0 * frequency;

            for (double x = 0; x < Ts; x += delta)
            {
                result.Add(Math.Sin(c * x + fi));
            }

            return result;
        }

        /// <summary>
        /// 余弦信号
        /// </summary>
        /// <param name="sampleRate">采样倍数</param>
        /// <param name="fi">初始相位</param>
        /// <returns></returns>
        static public List<double> Cos(int sampleRate,double fi)
        {
            int frequency = 1;

            List<double> result = new List<double>();

            double delta = 1.0 / frequency / sampleRate;
            double Ts = 1.0 / frequency;
            double c = Math.PI * 2.0 * frequency;

            for (double x = 0; x < Ts; x += delta)
            {
                result.Add(Math.Cos(c * x + fi));
            }

            return result;
        }

        
        /// <summary>
        /// 求正态分布的随机数
        /// </summary>
        /// <param name="lenth">随机数序列长度</param>
        /// <param name="amplitude">幅度</param>
        /// <returns>随机序列</returns>
        static public double[] AwgnGenerator(int lenth,double amplitude)
        {
            double[] f = new double[lenth];

            // 产生 [0,1] 间的均匀分布数据
            for (int i = 0; i < lenth; i++)
            {
                f[i] = (new System.Random(Guid.NewGuid().GetHashCode()).NextDouble());
            }

            double sum = 0,sum2=0;
            // 利用均匀分布产生正态分布数据
            for (int i = 0; i < f.Length; i++)
            {
                double a = 0, b = 0;
                a = Math.Sqrt((-2) * Math.Log(f[i], Math.E));
                b = Math.Cos(2 * Math.PI * f[i]);
                f[i] = (a * b * 0.3 + 1);
                sum += f[i];
                sum2 += (f[i] * f[i]);
            }

            return f;
        }
    }
}
